package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.admin.vo.AuthorityVO;
import com.cskaoyan.service.PermissionService;
import com.cskaoyan.bean.admin.vo.RoleOptionVO;
import com.cskaoyan.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: market_34th
 * @DESCRIPTION:
 * @Author: qiu
 * @DATE: 2021/10/18 23:41
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    /**
     * 角色分页信息
     * @param param
     * @return
     */
    @RequiresPermissions("admin:role:list")
    @GetMapping("/list")
    public BaseRespVo list(String name, BaseParam param) {

        BasePageData<Role> data = roleService.query(name, param);

        return BaseRespVo.ok(data);
    }

    /**
     * 角色信息选项
     * @return
     */
    @RequiresPermissions("admin:role:options")
    @GetMapping("/options")
    public BaseRespVo getOptions() {

        BasePageData<RoleOptionVO> data = roleService.getOptions();

        return BaseRespVo.ok(data);
    }

    /**
     * 添加角色
     */
    @RequiresPermissions("admin:role:create")
    @PostMapping("/create")
    public BaseRespVo create(@RequestBody Map map) {

        Role role = roleService.create(String.valueOf(map.get("name")), String.valueOf(map.get("desc")));

        if (role == null) {
            return BaseRespVo.fail(640, "角色名已存在");
        }

        return BaseRespVo.ok(role);
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:delete")
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Role role) {

        if (role == null || role.getId() == 1) {
            return BaseRespVo.fail("初始角色不能删除！");
        }
        roleService.delete(role);
        return BaseRespVo.ok();

    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequiresPermissions("admin:role:update")
    @PostMapping("/update")
    public BaseRespVo update(@RequestBody Role role) {
        if (role.getId() == 1) {
            return BaseRespVo.fail("初始角色不能修改！");
        }
        roleService.update(role);
        return BaseRespVo.ok();
    }

    /**
     * 获取三级联动数据、以及角色拥有的权限
     * @param roleId
     * @return
     */
    @RequiresPermissions("admin:role:permission:get")
    @GetMapping("/permissions")
    public BaseRespVo permissions(Integer roleId) {
        AuthorityVO authorityVO = roleService.permissions(roleId);
        return BaseRespVo.ok(authorityVO);
    }

    /**
     * 修改角色权限
     * @param map
     * @return
     */
    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("/permissions")
    public BaseRespVo permissions(@RequestBody Map map){

        Integer roleId = (Integer) map.get("roleId");
        List<String> permissions = (List<String>) map.get("permissions");

        if(roleId == 1){
            return BaseRespVo.fail("超级管理员的权限不能修改！");
        }

        permissionService.updatePermsByRoleId(roleId, permissions);

        return BaseRespVo.ok();
    }


}